﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using NExt.Core.Model.Enum;
using NExt.data;
using NExt.form;
using NExt.grid;
using SortDirection = NExt.data.SortDirection;

namespace NExt.Demo.Application.Entity2
{
    public class BuySellEdit : NExt.UI.Edit.Base.EditPresenterBase
    {
        private TabPanel mainTabPanel;
        private Panel mainTransactionTab;
        private Panel amountTab;
        private Panel transactionDetailTab;
        private Panel taxDetailTab;
        private Panel stateListTab;
        private Panel transactionFlagsTab;
        private FieldSet buyAmountFieldSet;
        private FieldSet sellAmountFieldSet;

        private TextField txtReferenceNo;
        private TextField txtTrader;
        private DateField dtfInputDate;
        private TextField txtAccountNo;
        private TextField txtInstructions;
        private TextField txtPostingAcc;
        private TriggerField txtCounterparty;
        private ComboBox cmbPortfolio;
        private ComboBox cmbType;
        private ComboBox cmbInstrument;
        private RadioGroup rbTransactionType;
        private TextField txtReason;
        private TextField txtBuy;
        private ComboBox cmbBuyCurrency;
        private NumberField nmbBuyRate;
        private TextField txtSell;
        private ComboBox cmbSellCurrency;
        private NumberField nmbSellRate;
        private TextField txtBuyBuySell;
        private ComboBox cmbBuyBuySellCurrency;
        private NumberField nmbBuyBuySellRate;
        private TextField txtSellBuySell;
        private ComboBox cmbSellBuySellCurrency;
        private NumberField nmbSellBuySellRate;
        private TextField txtDealRate;
        private ComboBox cmbMarket;
        private DateField dtSettlementDate;
        private TextField txtBenchmarkRate;
        private TextField txtAprRate;
        private TextField txtMaturityRate;
        private TextField txtDealRateNet;
        private TextField txtReflectionRate;
        private TextField txtValueDate;
        private TextField txtImpliedRate;
        private TriggerField txtBroker;
        private DateField dtTradeDate;
        private TextField txtLastState;
        private TextField txtLastStateMaker;
        private TextField txtLastStateDate;
        private Store taxDetailStore;
        private GridPanel grdTaxDetail;
        private Store stateListStore;
        private GridPanel grdStateList;
        private Checkbox chkRateReasonability;
        private Checkbox chkTimeLag;
        private Checkbox chkCash;
        private Checkbox chkAccount;
        private Checkbox chkInstructionReceived;

        protected override void FormBuildComponents()
        {
            base.FormBuildComponents();

            mainTabPanel = new TabPanel();
            mainTransactionTab = new Panel();
            amountTab = new Panel();
            transactionDetailTab = new Panel();
            taxDetailTab = new Panel();
            stateListTab = new Panel();
            transactionFlagsTab = new Panel();
            buyAmountFieldSet = new FieldSet();
            sellAmountFieldSet = new FieldSet();

            txtReferenceNo = new TextField();
            txtReferenceNo = new TextField();
            txtTrader = new TextField();
            dtfInputDate = new DateField();
            txtAccountNo = new TextField();
            txtInstructions = new TextField();
            txtPostingAcc = new TextField();
            txtCounterparty = new TriggerField();
            cmbPortfolio = new ComboBox();
            cmbType = new ComboBox();
            cmbInstrument = new ComboBox();
            rbTransactionType = new RadioGroup();
            txtReason = new TextField();
            txtBuy = new TextField();
            cmbBuyCurrency = new ComboBox();
            nmbBuyRate = new NumberField();
            txtSell = new TextField();
            cmbSellCurrency = new ComboBox();
            nmbSellRate = new NumberField();
            txtBuyBuySell = new TextField();
            cmbBuyBuySellCurrency = new ComboBox();
            nmbBuyBuySellRate = new NumberField();
            txtSellBuySell = new TextField();
            cmbSellBuySellCurrency = new ComboBox();
            nmbSellBuySellRate = new NumberField();
            txtDealRate = new TextField();
            cmbMarket = new ComboBox();
            dtSettlementDate = new DateField();
            txtBenchmarkRate = new TextField();
            txtAprRate = new TextField();
            txtMaturityRate = new TextField();
            txtDealRateNet = new TextField();
            txtReflectionRate = new TextField();
            txtValueDate = new TextField();
            txtImpliedRate = new TextField();
            txtBroker = new TriggerField();
            dtTradeDate = new DateField();
            txtLastState = new TextField();
            txtLastStateMaker = new TextField();
            txtLastStateDate = new TextField();
            taxDetailStore = new Store();
            grdTaxDetail = new GridPanel();
            stateListStore = new Store();
            grdStateList = new GridPanel();
            chkRateReasonability = new Checkbox();
            chkTimeLag = new Checkbox();
            chkCash = new Checkbox();
            chkAccount = new Checkbox();
            chkInstructionReceived = new Checkbox();

        }

        protected override void FormInitializeComponents()
        {
            base.FormInitializeComponents();

            InitializeFormProperties();

            BuildMainTransactionTab();

            BuildAmountTab();

            BuildTransactionDetailTab();

            BuildTaxDetailTab();

            BuildStateInfoTab();

            BuildTransactionFlagsTab();

            formPanel.items.Add(mainTabPanel);
        }

        private void BuildMainTransactionTab()
        {
            Panel topColumnsPanel = new Panel();
            topColumnsPanel.border = false;
            //topColumnsPanel.defaults = new { bodyStyle = "padding:5px 5px" };
            topColumnsPanel.layout = "column";
            mainTransactionTab.items.Add(topColumnsPanel);

            Panel topColumnsLeftPanel = new Panel();
            topColumnsLeftPanel.border = false;
            topColumnsLeftPanel.layout = "form";
            topColumnsPanel.items.Add(topColumnsLeftPanel);

            Panel topColumnsRightPanel = new Panel();
            topColumnsRightPanel.border = false;
            topColumnsRightPanel.layout = "form";
            topColumnsPanel.items.Add(topColumnsRightPanel);

            topColumnsLeftPanel.items.Add(txtReferenceNo);
            topColumnsLeftPanel.items.Add(txtTrader);
            topColumnsLeftPanel.items.Add(dtfInputDate);

            topColumnsRightPanel.items.Add(txtAccountNo);
            topColumnsRightPanel.items.Add(txtInstructions);
            topColumnsRightPanel.items.Add(txtPostingAcc);

            topColumnsLeftPanel.items.Add(txtCounterparty);
            topColumnsLeftPanel.items.Add(cmbPortfolio);
            topColumnsLeftPanel.items.Add(cmbType);
            topColumnsLeftPanel.items.Add(cmbInstrument);
            topColumnsLeftPanel.items.Add(rbTransactionType);
            topColumnsLeftPanel.items.Add(txtReason);

            mainTabPanel.items.Add(mainTransactionTab);
        }

        private void BuildAmountTab()
        {
            Panel amountColumnPanel = new Panel();
            amountColumnPanel.border = true;
            //amountColumnPanel.defaults = new { anchor = "95%, 95%", bodyStyle = "padding:5px 5px" };
            amountColumnPanel.layout = "column";

            buyAmountFieldSet.items.Add(txtBuy);
            buyAmountFieldSet.items.Add(cmbBuyCurrency);
            buyAmountFieldSet.items.Add(nmbBuyRate);
            buyAmountFieldSet.items.Add(txtSell);
            buyAmountFieldSet.items.Add(cmbSellCurrency);
            buyAmountFieldSet.items.Add(nmbSellRate);
            amountColumnPanel.items.Add(buyAmountFieldSet);

            sellAmountFieldSet.items.Add(txtBuy);
            sellAmountFieldSet.items.Add(cmbBuyCurrency);
            sellAmountFieldSet.items.Add(nmbBuyRate);
            sellAmountFieldSet.items.Add(txtSell);
            sellAmountFieldSet.items.Add(cmbSellCurrency);
            sellAmountFieldSet.items.Add(nmbSellRate);
            amountColumnPanel.items.Add(sellAmountFieldSet);

            amountTab.items.Add(amountColumnPanel);

            mainTabPanel.items.Add(amountTab);
        }

        private void BuildTransactionDetailTab()
        {
            transactionDetailTab.items.Add(txtDealRate);
            transactionDetailTab.items.Add(cmbMarket);
            transactionDetailTab.items.Add(dtSettlementDate);
            transactionDetailTab.items.Add(txtBenchmarkRate);
            transactionDetailTab.items.Add(txtAprRate);
            transactionDetailTab.items.Add(txtMaturityRate);
            transactionDetailTab.items.Add(txtReflectionRate);
            transactionDetailTab.items.Add(txtValueDate);
            transactionDetailTab.items.Add(txtImpliedRate);
            transactionDetailTab.items.Add(txtBroker);
            transactionDetailTab.items.Add(dtTradeDate);
            transactionDetailTab.items.Add(txtLastState);
            transactionDetailTab.items.Add(txtLastStateMaker);
            transactionDetailTab.items.Add(txtLastStateDate);

            mainTabPanel.items.Add(transactionDetailTab);
        }

        private void BuildTaxDetailTab()
        {
            Button button = new Button();
            taxDetailTab.items.Add(button);

            //taxDetailTab.items.Add(grdTaxDetail);



            mainTabPanel.items.Add(taxDetailTab);
        }

        private void BuildStateInfoTab()
        {
            stateListTab.items.Add(grdStateList);

            mainTabPanel.items.Add(stateListTab);
        }

        private void BuildTransactionFlagsTab()
        {
            transactionFlagsTab.items.Add(chkRateReasonability);
            transactionFlagsTab.items.Add(chkTimeLag);
            transactionFlagsTab.items.Add(chkCash);
            transactionFlagsTab.items.Add(chkAccount);
            transactionFlagsTab.items.Add(chkInstructionReceived);

            mainTabPanel.items.Add(transactionFlagsTab);
        }

        private void InitializeFormProperties()
        {
            this.autoScroll = true;
            this.title = "Edit";
            //formPanel.defaults = new { bodyStyle = "padding:5px 5px" };
            //formPanel.border = false;
            //formPanel.extended = new { anchor = "95%, 95%" };

            mainTabPanel.layoutOnTabChange = true;
            mainTabPanel.defaults = BodyStyle.DEFAULT_PADDING;
            mainTabPanel.activeTab = 0;

            mainTransactionTab.title = "Main Transaction";
            mainTransactionTab.autoHeight = true;
            mainTransactionTab.autoWidth = true;
            mainTransactionTab.layout = "form";

            amountTab.title = "Amount";
            amountTab.autoHeight = true;
            amountTab.autoWidth = true;
            amountTab.layout = "form";

            transactionDetailTab.title = "Transaction Detail";
            transactionDetailTab.autoHeight = true;
            transactionDetailTab.autoWidth = true;
            transactionDetailTab.layout = "form";

            taxDetailTab.title = "Tax Detail";
            taxDetailTab.bodyStyle = "padding:0px";
            taxDetailTab.autoHeight = true;
            taxDetailTab.autoWidth = true;
            taxDetailTab.layout = "form";

            stateListTab.title = "State List";
            stateListTab.autoHeight = true;
            stateListTab.autoWidth = true;
            stateListTab.layout = "form";

            transactionFlagsTab.title = "Transaction Flags";
            transactionFlagsTab.autoHeight = true;
            transactionFlagsTab.autoWidth = true;
            transactionFlagsTab.layout = "form";

            buyAmountFieldSet.title = "Buy";
            sellAmountFieldSet.title = "Sell";

            //grdTaxDetail.hideLabel = true;

            //grdStateList.hideLabel = true;

            txtReferenceNo.fieldLabel = "Reference No";
            txtReferenceNo.name = "LogicalReference";
            txtTrader.fieldLabel = "Trader";
            //txtTrader.name = "Trader";
            dtfInputDate.fieldLabel = "Input Date";
            dtfInputDate.name = "InputDate";
            txtAccountNo.fieldLabel = "Account No";
            //txtAccountNo.name = "Account No";
            txtInstructions.fieldLabel = "Instructions";
            //txtInstructions.name = "Instructions";
            txtPostingAcc.fieldLabel = "Posting Account";
            //txtPostingAcc.name = "Posting Account";
            txtCounterparty.fieldLabel = "Counterparty";
            //txtCounterparty.name = "Counterparty";
            cmbPortfolio.fieldLabel = "Portfolio.ID";
            //txtPortfolio.name = "Portfolio.ID";
            cmbType.fieldLabel = "Type";
            cmbType.name = "ProductSubType";
            cmbInstrument.fieldLabel = "Instrument";
            cmbInstrument.name = "Instrument.Name";
            rbTransactionType.fieldLabel = "Transaction Type";
            rbTransactionType.name = "TransactionTypeId";

            Radio buy = new Radio();
            buy.name = "Type";
            buy.boxLabel = "Buy";
            buy.value = "Buy";
            Radio sell = new Radio();
            sell.name = "Type";
            sell.boxLabel = "Sell";
            sell.value = "Sell";
            rbTransactionType.items.Add(buy);
            rbTransactionType.items.Add(sell);

            txtReason.fieldLabel = "Reason";
            txtReason.name = "Reason";
            txtBuy.fieldLabel = "Buy";
            cmbBuyCurrency.fieldLabel = "Buy Currency";
            nmbBuyRate.fieldLabel = "Buy Rate";
            //txtBuy.name = "Buy";
            txtSell.fieldLabel = "Sell";
            cmbSellCurrency.fieldLabel = "Sell Currency";
            nmbSellRate.fieldLabel = "Sell Rate";
            //txtSell.name = "Sell";
            txtBuyBuySell.fieldLabel = "Buy Currency";
            cmbBuyBuySellCurrency.fieldLabel = "Buy Currency";
            nmbBuyBuySellRate.fieldLabel = "Buy Currency";
            txtSellBuySell.fieldLabel = "Sell Currency";
            cmbSellBuySellCurrency.fieldLabel = "Sell Currency";
            nmbSellBuySellRate.fieldLabel = "Sell Currency";
            //txtTropsBreakEven.name = "Trops Break Event";
            txtDealRate.fieldLabel = "Deal Rate";
            txtDealRate.name = "DealRateGross";
            cmbMarket.fieldLabel = "Market";
            cmbMarket.name = "MarketId";
            dtSettlementDate.fieldLabel = "SettlementDate";
            dtSettlementDate.name = "SettlementDate";
            txtBenchmarkRate.fieldLabel = "Benchmark Rate";
            txtBenchmarkRate.name = "BenchmarkRate";
            txtAprRate.fieldLabel = "Apr Rate";
            txtAprRate.name = "AprRate";
            txtMaturityRate.fieldLabel = "Maturithy Rate";
            txtMaturityRate.name = "MaturithyRate";
            txtDealRateNet.fieldLabel = "Deal Rate Net";
            txtDealRateNet.name = "DealRateNet";
            txtReflectionRate.fieldLabel = "Reflection Rate";
            txtReflectionRate.name = "ReflectionRate";
            txtValueDate.fieldLabel = "Value Rate";
            txtValueDate.name = "ValueRate";
            txtImpliedRate.fieldLabel = "Implied Rate";
            txtImpliedRate.name = "ImpliedRate";
            txtBroker.fieldLabel = "Broker";
            txtBroker.name = "Broker";
            dtTradeDate.fieldLabel = "Trade Date";
            dtTradeDate.name = "TradeDate";
            txtLastState.fieldLabel = "Last State";
            txtLastState.name = "StateId";
            txtLastStateMaker.fieldLabel = "Last State Maker";
            txtLastStateMaker.name = "MakerId";
            txtLastStateDate.fieldLabel = "Last State Date";
            txtLastStateDate.name = "StateDate";

            //Tax Detail Grid
            Record taxDetailRecord = new Record();
            taxDetailRecord.fields.Add(new FieldDefinition() { name = "taxID", type = FieldDefinitionType.auto, sortDir = SortDirection.ASC });
            taxDetailRecord.fields.Add(new FieldDefinition() { name = "tax", type = FieldDefinitionType.floatt });

            JsonReader taxDetailReader = new JsonReader();
            taxDetailReader.recordType = taxDetailRecord;

            ColumnModel taxDetailColumnModel = new ColumnModel();
            taxDetailColumnModel.defaultSortable = true;
            taxDetailColumnModel.columns.Add(new RowNumberer());
            taxDetailColumnModel.columns.Add(new Column { header = "Tax ID", width = 70, sortable = true, dataIndex = "Tax ID" });
            taxDetailColumnModel.columns.Add(new Column { header = "Tax", width = 70, sortable = true, dataIndex = "Tax" });

            taxDetailStore.reader = taxDetailReader;
            grdTaxDetail.layout = "fit";
            grdTaxDetail.border = false;
            grdTaxDetail.ds = taxDetailStore;
            grdTaxDetail.cm = taxDetailColumnModel;
            grdTaxDetail.viewConfig = new NExt.grid.GridView() { forceFit = true };
            grdTaxDetail.extended = new { anchor = "100%, 100%" };

            //StateList grid
            Record stateListRecord = new Record();
            stateListRecord.fields.Add(new FieldDefinition() { name = "Maker", type = FieldDefinitionType.intt, sortDir = SortDirection.ASC });
            stateListRecord.fields.Add(new FieldDefinition() { name = "Date", type = FieldDefinitionType.date });
            stateListRecord.fields.Add(new FieldDefinition() { name = "State", type = FieldDefinitionType.intt });

            JsonReader stateListReader = new JsonReader();
            stateListReader.recordType = taxDetailRecord;

            ColumnModel stateListColumnModel = new ColumnModel();
            stateListColumnModel.defaultSortable = true;
            stateListColumnModel.columns.Add(new RowNumberer());
            stateListColumnModel.columns.Add(new Column { header = "Maker", width = 70, sortable = true, dataIndex = "Maker" });
            stateListColumnModel.columns.Add(new Column { header = "Date", width = 70, sortable = true, dataIndex = "Date" });
            stateListColumnModel.columns.Add(new Column { header = "State", width = 70, sortable = true, dataIndex = "State" });

            stateListStore.reader = stateListReader;
            grdStateList.layout = "fit";
            grdStateList.border = false;
            grdStateList.ds = stateListStore;
            grdStateList.cm = stateListColumnModel;
            grdStateList.viewConfig = new NExt.grid.GridView() { forceFit = true };
            grdStateList.extended = new { anchor = "100%, 100%" };

            chkRateReasonability.fieldLabel = "Rate Reasonability";
            chkTimeLag.fieldLabel = "Time Lag";
            chkCash.fieldLabel = "Cash";
            chkAccount.fieldLabel = "Account";
            chkInstructionReceived.fieldLabel = "Instruction Received";
        }
    }
}
